const path = require("path");
const eslintWebpackPlugin = require("eslint-webpack-plugin");
const htmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");

function getStyleLoader(pre) {
  return [
    // 执行顺序：从右到左，或从下到上
    // "style-loader", // style-loader: 将js中的css通过创建style标签添加到html文件中
    MiniCssExtractPlugin.loader,
    "css-loader", // css-loader: 将css资源编译成commonjs的模块到js中
    {
      loader: "postcss-loader",
      options: {
        postcssOptions: {
          plugins: [
            [
              "postcss-preset-env",
              {
                // Options
              },
            ],
          ],
        },
      },
    },
    pre,
  ].filter(Boolean);
}

module.exports = {
  // 入口
  entry: "./core/src/index.js",
  // 输出
  output: {
    // 所有文件的输出路径
    // path: path.resolve(__dirname, "../dist/"), // 绝对路径
    path: path.resolve(__dirname, "../../dist/"), // 绝对路径
    filename: "scripts/main.js",
    publicPath: "../",
    // 自动清空上次打包内容
    // 原理：打包前，清空path目录
    clean: true,
  },
  // 加载器
  module: {
    rules: [
      {
        test: /\.css$/i, // 只检测以.css结尾的文件
        use: getStyleLoader(),
      },
      {
        test: /\.less$/i,
        use: getStyleLoader("less-loader"),
      },
      {
        test: /\.(png|jpe?g|gif|webp)$/i,
        type: "asset",
        parser: {
          dataUrlCondition: {
            maxSize: 10 * 1024, // 10kb
          },
        },
        generator: {
          filename: "static/images/[hash:10][ext][query]",
        },
      },
      {
        test: /\.(ttf|woff2?)$/i,
        type: "asset/resource",
        generator: {
          filename: "static/iconfonts/[hash:10][ext][query]",
        },
      },
      {
        test: /\.(mp3|mp4)$/i,
        type: "asset/resource",
        generator: {
          filename: "static/medias/[hash:10][ext][query]",
        },
      },
      {
        test: /\.html$/i,
        loader: "html-loader",
        options: {
          sources: true,
        },
      },
      {
        test: /\.(?:js|mjs|cjs)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: {
            presets: [["@babel/preset-env", { targets: "defaults" }]],
          },
        },
      },
    ],
  },
  /* optimization: {
    minimizer: [
      // For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`), uncomment the next line
      // `...`,
      new CssMinimizerPlugin(),
    ],
  }, */
  plugins: [
    new htmlWebpackPlugin({
      template: path.resolve(__dirname, "../public/index.html"),
      filename: "public/index.html",
    }),
    new eslintWebpackPlugin({
      context: path.resolve(__dirname, "./core/src/"),
      configType: "flat",
      cache: true,
      cacheLocation: "./.eslintcache",
    }),
    new MiniCssExtractPlugin({
      filename: "styles/main.css",
    }),
    new CssMinimizerPlugin(),
  ],
  mode: "production",
};
